# !/usr/bin python3
# encoding: utf-8 -*-
# @author: 半夏 
# 微信：testfankevin
# @Time: 2023/11/7 21:23
# @Copyright：北京码同学
# 订单流程 登录-设置收货地址-设置付款方式-立即购买-添加购物车-创建交易
import time

import pytest
import requests

def buyer_login(username='mtx0327',password='fcea920f7412b5da7be0cf42b8c93759'):
    '''买家登录'''
    url = 'http://82.156.74.26:7002/passport/login'
    # 查询参数
    params = {'username':username,
              'password':password,
              'captcha':1512,
              'uuid':'be95c490-6768-11ee-9361-71d25890de14'}
    resp = requests.post(url=url,params=params)
    # 响应状态码
    # code = resp.status_code
    # print(f'响应状态码:{code}')
    # # 响应体数据 json
    json = resp.json()
    # 1 先提取access_token字段值 声明全局变量
    global token
    token = json['access_token']
    return resp

def accept_address(address_id=11420):
    '''收货地址'''
    url = f'http://82.156.74.26:7002/trade/checkout-params/address-id/{address_id}'
    # 增加头信息
    headers = {'Authorization':token}
    resp = requests.post(url=url,headers=headers)
    return resp

def pay_type():
    '''付款方式'''
    url = 'http://82.156.74.26:7002/trade/checkout-params/payment-type'
    data = {'payment_type':'COD'}
    headers = {'Authorization': token}
    resp = requests.post(url=url,data=data,headers=headers)
    return resp
def buy_now(sku_id=541,num=1):
    '''立即购买'''
    url = 'http://82.156.74.26:7002/trade/carts/buy'
    headers = {'Authorization': token}
    # 这个sku_id不是商品id 是产品id mtxshp_goods对应es_sku表中
    params = {'sku_id':sku_id,'num':num}
    resp = requests.post(url=url,headers=headers,params=params)
    return resp

def add_cart(sku_id=541,num=1):
    '''添加购物车'''
    url= 'http://82.156.74.26:7002/trade/carts'
    data = {'sku_id':sku_id,'num':num}
    headers = {'Authorization': token}
    resp = requests.post(url=url,data=data,headers=headers)
    return resp


def create_trade(client='PC',way='BUY_NOW'):
    '''创建交易'''
    # 订单数据来源way
    # way=BUY_NOW 创建交易时会从立即购买接口数据创建订单
    # way=CART 创建交易时会从添加购物车接口数据创建订单
    # 立即购买接口 成功后将订单数据存储在redis缓存中
    # 创建交易会从redis缓存读取对应的数据完成订单,同时清除redis数据
    url = 'http://82.156.74.26:7002/trade/create'
    data = {'client':client,'way':way}
    headers = {'Authorization': token}
    resp = requests.post(url=url,data=data,headers=headers)
    # 提取 trade_sn 字段值
    global trade_sn
    trade_sn = resp.json()['trade_sn']
    return resp


# # 卖家登录 - 卖家发货 - 买家确认收货 - 卖家确认收款
def seller_login(username='shamoseller',password='fafd8b5f5d11048e8078b66ab075acb3'):
    '''卖家登录'''
    url = 'http://82.156.74.26:7003/seller/login'
    params = {'username':username,
            'password':password,
            'captcha':1512,
            'uuid':'ddacdaf0-753a-11ee-98d5-1d124f0f5b02'}
    resp = requests.get(url=url,params=params)
    global seller_token
    seller_token = resp.json()['access_token']
    return resp
def seller_delivery(trade_sn):
    '''卖家发货'''
    url = f'http://82.156.74.26:7003/seller/trade/orders/{trade_sn}/delivery'
    data = {'ship_no':123,'logi_id':13,'logi_name':'中通01'}
    headers = {'Authorization':seller_token}
    resp = requests.post(url=url,data=data,headers=headers)
    return resp

def rog_order(trade_sn):
    url = f'http://82.156.74.26:7002/trade/orders/{trade_sn}/rog'
    headers = {'Authorization': token}
    resp = requests.post(url=url, headers=headers)
    return resp

def seller_pay(trade_sn):
    url = f'http://82.156.74.26:7003/seller/trade/orders/{trade_sn}/pay'
    headers = {'Authorization': seller_token}
    data = {'pay_price':99}
    resp = requests.post(url=url,headers=headers,data=data)
    return resp

if __name__ == '__main__':
    resp = buyer_login()
    # 响应状态码
    code = resp.status_code
    print(f'响应状态码:{code}')
    json = resp.json()
    print(f'响应体内容json：{json}')

    # 收货地址
    resp = accept_address()
    # 响应状态码
    code = resp.status_code
    print(f'响应状态码:{code}')
    # 响应体数据text
    text = resp.text
    print(f'响应体内容文本:{text}')
    # 设置货到付款
    resp = pay_type()
    # 响应状态码
    code = resp.status_code
    print(f'响应状态码:{code}')
    # 响应体数据text
    text = resp.text
    print(f'响应体内容文本:{text}')
    # 立即购买
    resp = buy_now()
    # 响应状态码
    code = resp.status_code
    print(f'响应状态码:{code}')
    # 响应体数据text
    text = resp.text
    print(f'响应体内容文本:{text}')
    # # 添加购物车
    # resp = add_cart()
    # # 响应状态码
    # code = resp.status_code
    # print(f'响应状态码:{code}')
    # resp = create_trade()
    #
    # # 响应状态码
    # code = resp.status_code
    # print(f'响应状态码:{code}')
    # # 响应体数据text
    # text = resp.text
    # print(f'响应体内容文本:{text}')
    # resp = seller_login()
    # code = resp.status_code
    # print(f'响应状态码:{code}')
    # json = resp.json()
    # print(f'响应体内容json：{json}')
    #
    # resp = seller_delivery(trade_sn=trade_sn)
    # code = resp.status_code
    # print(f'响应状态码:{code}')
    # text = resp.text
    # print(f'响应体内容文本:{text}')
    #
    #
    # resp = rog_order(trade_sn=trade_sn)
    # code = resp.status_code
    # print(f'响应状态码:{code}')
    # text = resp.text
    # print(f'响应体内容文本:{text}')
    #
    # resp = seller_pay(trade_sn=trade_sn)
    #
    # code = resp.status_code
    # print(f'响应状态码:{code}')
    # text = resp.text
    # print(f'响应体内容文本:{text}')
